//
// Created by A4-28 on 2020/10/14.
//
#include <iostream>
#include "entt/entt.hpp"
struct position {
    float x;
    float y;
};

struct velocity {
    float dx;
    float dy;
};

void update(entt::registry &registry) {
    auto view = registry.view<position, velocity>();

    for(auto entity: view) {
        // gets only the components that are going to be used ...
        auto &vel = view.get<velocity>(entity);
        vel.dx = 0.;
        vel.dy = 0.;
    }
}

void update(std::uint64_t dt, entt::registry &registry) {
    registry.view<position, velocity>().each([dt](auto &pos, auto &vel) {
        pos.x += vel.dx * dt;
        pos.y += vel.dy * dt;
    });
}

int main(){

    entt::registry registry;

    std::uint64_t dt = 16;

    for(auto i = 0; i < 10; ++i) {
        //实例化
        auto entity = registry.create();
        //初始化给定组件并将其分配给实体
        registry.emplace<position>(entity, i * 1.f, i * 1.f);
        if(i % 2 == 0) { registry.emplace<velocity>(entity, i * .1f, i * .1f); }

        //销毁
        registry.destroy(entity);
    }
    update(dt, registry);
    update(registry);

    // destroys all the entities in a range
    //view 得到所有的entt

    auto view = registry.view<position, velocity>();
    registry.destroy(view.begin(), view.end());

//    // 校验entity是否有效
//    bool b = registry.valid(entity);
//
//   // 获取entity version
//    auto version = registry.version(entity);
//
//   // 获取当前entity的version
//    auto curr = registry.current(entity);


}